package cn.smilefox.service.impl;

import cn.smilefox.dao.CourseDao;
import cn.smilefox.dao.RoleDao;
import cn.smilefox.dao.TeacherDao;
import cn.smilefox.domain.Course;
import cn.smilefox.domain.Role;
import cn.smilefox.domain.Teacher;
import cn.smilefox.service.CourseService;

import java.util.List;


public class CourseServiceImpl implements CourseService {

//    Long test_num = 0L;
//    Long theory_num = 0L;

    private CourseDao courseDao;

    public void setCourseDao(CourseDao courseDao) {
        this.courseDao = courseDao;
    }

    private TeacherDao teacherDao;

    public void setTeacherDao(TeacherDao teacherDao) {
        this.teacherDao = teacherDao;
    }

    private RoleDao roleDao;

    public void setRoleDao(RoleDao roleDao) {
        this.roleDao = roleDao;
    }

    //查询所有课实验程
    @Override
    public List<Course> findalltest() {

        List<Course> courseList = courseDao.findalltest();

        // 取出每个courseList里面的id属性去获取对应的teacher属性出来
        for (Course course_s : courseList) {
            Long id = course_s.getId();
            List<Teacher> teachers = teacherDao.findTeacherById(id);
            course_s.setTeachers(teachers);
        }

        return courseList;
    }

    //添加实验课程
    @Override
    public void save(Course course) {
        //将课程信息录入数据库
        Long c_id = courseDao.save(course);
        //将课程对应的教师关系录入数据库
        String[] teacherS = course.getTeacher().split("，");
        for (String teacher : teacherS) {
            //查询每个老师id
            Long t_id = courseDao.findTeacherByName(teacher);
            //将对应课程id和老师id存入数据库表
            courseDao.saveTeacherTestRel(t_id,c_id);
        }
    }

    //删除实验课程
    @Override
    public void testdel(Long courseid) {
        //删除关系表课程相关数据
        courseDao.delTeacherTestRel(courseid);
        //删除课程数据
        courseDao.deltest(courseid);
    }

    //查找实验课程
    @Override
    public List<Course> findtest(Course course) {
        List<Course> courseList = courseDao.findtest(course);
        //取出封装的每个courselist里面的course的id获取其teacher属性。
        for (Course course1_s : courseList) {
        //获得teacher的id
            Long id = course1_s.getId();
        //id作为参数查询对于的role集合数据
            List<Teacher> teachers = teacherDao.findTeacherById(id);
            course1_s.setTeachers(teachers);
        }
        return courseList;
    }

    //查询所有理论课程
    @Override
    public List<Course> findalltheory() {
        List<Course> courseList = courseDao.findalltheory();

        // 取出每个courseList里面的id属性去获取对应的teacher属性出来
        for (Course course_s : courseList) {
            Long id = course_s.getId();
            List<Teacher> teachers = teacherDao.findTeacherById2(id);
            course_s.setTeachers(teachers);
        }

        return courseList;
    }

    //保存理论课程
    @Override
    public void savetheory(Course course) {
        //将课程信息录入数据库
        Long c_id = courseDao.savetheory(course);
        //将课程对应的教师关系录入数据库
        String[] teacherS = course.getTeacher().split("，");
        for (String teacher : teacherS) {
            //查询每个老师id
            Long t_id = courseDao.findTeacherByName(teacher);
            //将对应课程id和老师id存入数据库表
            courseDao.saveTeacherTheoryRel(t_id,c_id);
        }
    }

    //删除理论课程
    @Override
    public void theorydel(Long courseid) {
        //删除关系表课程相关数据
        courseDao.delTeacherTheoryRel(courseid);
        //删除课程数据
        courseDao.deltheory(courseid);

    }

    //查询理论课程
    @Override
    public List<Course> findtheory(Course course) {
        List<Course> courseList = courseDao.findtheory(course);
        //取出封装的每个courselist里面的course的id获取其teacher属性。
        for (Course course1_s : courseList) {
            //获得teacher的id
            Long id = course1_s.getId();
            //id作为参数查询对于的role集合数据
            List<Teacher> teachers = teacherDao.findTeacherById2(id);
            course1_s.setTeachers(teachers);
        }
        return courseList;
    }

    @Override
    public List<Course> findallnum() {
        return null;
    }


//    //将所有课程查询出来并且统计
//    @Override
//    public List<Course> findallnum() {
//
//         Long test_num = 0L;
//         Long theory_num = 0L;
//        //查询各个教室实验课时
//        //1、将所有老师进行查询封装。
//        List<Teacher> teacherList = teacherDao.findall();
//        //2.循环进行读取id并且通过关联表进行查询相关课程
//        for (Teacher teacher : teacherList) {
//            Long id = teacher.getId();
//            //3、循环将该教室所有有关课程的实验课时提取出来
//                    //--------------实验课程课时添加------------------//
//            List<Course> courseList = courseDao.findTestById(id);
//            for (Course course : courseList) {
//                String course_num = course.getCourse_num();
//                Long t = Long.valueOf(course_num);
//                test_num += t;
//            }
//            teacher.setTest_num(test_num);
//            //---------理论课程课时添加---------------//
//            List<Course> courseList2 = courseDao.findTheoryById(id);
//            for (Course course : courseList2) {
//                String course_num = course.getCourse_num();
//                Long t = Long.valueOf(course_num);
//                theory_num += t;
//            }
//            //4、将所有课时进行相加封装进每个course的test_num.
//            teacher.setTheory_num(theory_num);
//        }
//        return teacherList;
//    };


}
